<?php

/**
 *  @FileComment            [前台列表页控制器]
 *  @ProjectName             项目名称：ShopFrame
 *  @FileName                   文件名称：ListController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-4 21:39:57
 *  *
 */
class ListController extends CommonController {

        public function index() {
                $sort = Q('get.sort', 'all');
                $cid = Q('get.cid', 0, 'intval');
                $onlyHave = Q('get.stock');
                $stock = '';
                $price = 'priceDesc';
                //当前在哪个分类
                $this->cate($cid);
                //顶级和二级分类，列表左侧的可收缩菜单
                $this->otCate($cid);
                //所有商品列表显示区域
                $param = explode('_', Q('get.param'));
                $interParam = '$gidArr=array_intersect(';
                $tempGids = array();
                foreach ($param as $k => $v) {
                        if ($v) {
                                $gids[$k] = K('GoodsAttr')->query('SELECT DISTINCT gatwo.goods_gid FROM jd_goods_attr as gaone JOIN jd_goods_attr as gatwo on gaone.ganame=gatwo.ganame  where gaone.gaid=' . $v);
                                foreach ($gids[$k] as $value) {
                                        $tempGids[$k][] = $value['goods_gid'];
                                }
                                $interParam.='$tempGids[' . $k . ']' . ',';
                        }
                }
                $interParam = rtrim($interParam, ',');
                $interParam = $interParam . ');';
                $bid = Q('get.bid', 0, 'intval');
                $tid = Q('get.tid', 0, 'intval');
                $where = '';
                if ($cid) {
                        $where = "category_cid= $cid";
                }
                if ($tid) {
                        $where = 'type_tid=' . $tid;
                }
                if ($bid) {
                        $where.=" and brand_bid=$bid";
                }
                //库存筛选来回切换
                if ($onlyHave == 'stock') {
                        $where.=' and stock>0';
                        $stock = 'all';
                } else {
                        $stock = 'stock';
                }
                //如果不传。默认让库存为所有显示
                if (!$onlyHave) {
                        $stock = 'stock';
                }
                $this->assign('stock', $stock);
                //多选和单选分别的操作
                switch (count($tempGids)) {
                        case 0:
                                $where = $where;
                                break;
                        case 1:
                                foreach ($tempGids as $v) {
                                        $tempGids = $v;
                                }
                                $tempGids = implode(',', $tempGids);
                                $where.=' and gid IN(' . $tempGids . ')';
                                break;
                        default:
                                eval($interParam);
                                $gidArr = implode(',', $gidArr);
                                if ($gidArr) {
                                        $where.=' and gid IN(' . $gidArr . ')';
                                } else {
                                        $where.=' and gid=""';
                                }
                                break;
                }
                switch ($sort) {
                        case 'priceDesc':
                                $price = 'priceAsc';
                                $where.=' order by jdprice DESC';
                                break;
                        case 'priceAsc':
                                $price = 'priceDesc';
                                $where.=' order by jdprice ASC';
                                break;
                        case 'commentnum':
                                break;
                        case 'totalsales':
                                $where.=' order by addtime';
                                break;
                        default :
                }
                $this->assign('price', $price);
                $goodlist = K('Goods')->where("$where")->all();
                //左侧分类类型点击筛选
                $cateTypeInfo = K('CateTypeBrandView');
                if ($cid) {
                        $brandInfo = $cateTypeInfo->get_cid_data($cid);
                        $gid = K('goods')->get_gid(array('category_cid' => $cid));
                        if (!$gid) {
                                $gid = array(0);
                        }
                } else {
                        $tid = Q('get.tid', 0, 'intval');
                        $brandInfo = $cateTypeInfo->get_tid_data($tid);
                        $gid = K('goods')->get_gid(array('type_tid' => $tid));
                        if (!$gid) {
                                $gid = array(0);
                        }
                }
                //右侧类型属性的筛选
                $gid = implode(', ', $gid);
                //商品规格在列表页显示出来
                $attrs = K('GoodsTypeAttrGoodsAttrView')->get_all_data('goods_gid IN(' . $gid . ') and class = 1', 'ganame');
                //将商品规格处理列出来
                $tempOne = array();
                foreach ($attrs as $v) {
                        $tempOne[$v['type_attr_taid']][] = $v;
                }
                $typeAttr = K('TypeAttr');
                $tempTwo = array();
                foreach ($tempOne as $k => $v) {
                        $taname = $typeAttr->get_taname_data(array('taid' => $k));
                        $tempTwo[] = array(
                            'taname' => $taname['taname'],
                            'tavalue' => $v
                        );
                }
                //多选筛选处理
                $num = !empty($tempTwo) ? count($tempTwo) : 1;
                $param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, '0');
                $this->assign('param', $param);
                $this->assign('brandInfo', $brandInfo);
                $goodsComb = K('GoodsList');
                foreach ($goodlist as $k => $v) {
                        $temp = $goodsComb->get_Field_data(array('combattrid' => $v['comb']));
                        $goodlist[$k]['glid'] = $temp['glid'];
                }
                $this->assign('attrInfo', $tempTwo);
                $this->assign('goodlist', $goodlist);
                $this->display();
        }

        public function search() {
                $goods = K('Goods');
                $searchName = Q('post.search');
                $searchNames = String::splitWord($searchName);
                $goodlist = array();
                $searchAll = '(';
                //筛选出的记录
                $key='';
                foreach ($searchNames as $k => $v) {
                        $searchAll.=$k . '|';
                        $key.='gname like "%'.$k.'%" and ';
                }
                $key=rtrim($key,' and ');
                $goodlist = $goods->get_like_data(array($key));
                $searchAll = rtrim($searchAll, '|');
                $searchAll.=')';
                $this->assign('searchAll', $searchAll);
                $goodItem = K('GoodsList');
                foreach ($goodlist as $key => $v) {
                        $temp = $goodItem->get_Field_data(array('combattrid' => $v['comb']));
                        $goodlist[$key]['glid'] = $temp['glid'];
                }
                $this->assign('goodslist', $goodlist);
                $this->assign('serchName', $searchName);
                $this->display();
        }

}
